package com.bluefish.bltuan.tools;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public final class DateUtils {
	
	private static final long DAY = 24*3600*1000;
	
	private static final long HOUR = 3600*1000;
	
	private static final long MINUTE = 60*1000;
	
	private static final long SECOND = 1000;
	
	private static final String TIME_FORMAT = "{0}天{1}小时{2}分{3}秒";

	private DateUtils(){
		
	}
	
	public static String getDateString(String pattern, Date date){
		if(date != null){
			DateFormat format = new SimpleDateFormat(pattern);
			return format.format(date);
		}
		return null;
	}
	
	public static String getDateString(Date date){
		if(date != null){
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			return format.format(date);
		}
		return null;
	}
	
	public static Date getDate(String dateString){
		if(dateString != null){
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try {
				return format.parse(dateString);
			} catch (ParseException e) {
				return null;
			}
		}
		return null;
	}

	public static Date getTomorrow() {
		Date date = new Date();
		Date tomorrow = org.apache.commons.lang.time.DateUtils.addDays(date, 1);
		String dateStr = getDateString(tomorrow);
		return getDate(dateStr);
	}
	
	public static String sub(Date fromDate, Date toDate){
		return sub(fromDate, toDate, TIME_FORMAT);
	}
	
	public static String sub(Date fromDate, Date toDate, String format){
		long time = toDate.getTime() - fromDate.getTime();
		int day = (int)(time/DAY);
		time = time - day * DAY;
		int hour = (int)(time/HOUR);
		time = time - hour * HOUR;
		int minute = (int)(time/MINUTE);
		time = time - minute * MINUTE;
		int second = (int)(time/SECOND);
		return MessageFormat.format(format, day,hour,minute,second);
	}
	
	public static String remain(Date toDate, String format){
		return sub(new Date(), toDate, format);
	}
	
	public static String remain(Date toDate){
		return sub(new Date(), toDate);
	}
}
